import pandas as pd
import matplotlib.pyplot as plt

# 读取数据
file_path = r"C:\Users\luojinying\Desktop\world_pop_mig_186_countries.csv"
data = pd.read_csv(file_path)

# 查看数据基本信息，检查是否有缺失值
print(data.info())
print(data.head())

# 数据清洗
# 确保年份、人口和迁移人口列的数据类型正确
data['year'] = pd.to_numeric(data['year'], errors='coerce')
data['population'] = pd.to_numeric(data['population'], errors='coerce')
data['netMigration'] = pd.to_numeric(data['netMigration'], errors='coerce')

# 去除可能的缺失值
data.dropna(subset=['year', 'population', 'netMigration'], inplace=True)

# 筛选出阿富汗和澳大利亚的数据
afghanistan_data = data[data['country'] == 'Afghanistan']
australia_data = data[data['country'] == 'Australia']

# 设置图表
plt.figure(figsize=(14, 8))
plt.plot(afghanistan_data['year'], afghanistan_data['population'], label='Afghanistan Population', color='blue')
plt.plot(afghanistan_data['year'], afghanistan_data['netMigration'], label='Afghanistan Net Migration', color='green')
plt.plot(australia_data['year'], australia_data['population'], label='Australia Population', color='red')
plt.plot(australia_data['year'], australia_data['netMigration'], label='Australia Net Migration', color='purple')

# 添加图例
plt.legend()

# 添加标题和标签
plt.title('Population and Net Migration Trends: Afghanistan vs Australia (1960-2023)')
plt.xlabel('Year')
plt.ylabel('Population / Net Migration')

# 显示图表
plt.grid(True)
plt.show()

import pandas as pd
import matplotlib.pyplot as plt

# 读取数据
file_path = r"C:\Users\luojinying\Desktop\world_pop_mig_186_countries.csv"
data = pd.read_csv(file_path)

# 数据清洗
# 确保年份、人口和迁移人口列的数据类型正确
data['year'] = pd.to_numeric(data['year'], errors='coerce')
data['population'] = pd.to_numeric(data['population'], errors='coerce')
data['netMigration'] = pd.to_numeric(data['netMigration'], errors='coerce')

# 去除可能的缺失值
data.dropna(subset=['year', 'population', 'netMigration'], inplace=True)

# 筛选出阿富汗和澳大利亚的数据
afghanistan_data = data[(data['country'] == 'Afghanistan') & (data['year'] == 2023)]
australia_data = data[(data['country'] == 'Australia') & (data['year'] == 2023)]

# 设置图表
fig, ax = plt.subplots(figsize=(10, 6))

# 绘制柱状图
afghanistan_bar = ax.bar('Afghanistan', afghanistan_data['population'].values[0], label='Afghanistan Population', color='blue')
afghanistan_migration_bar = ax.bar('Afghanistan', afghanistan_data['netMigration'].values[0], bottom=afghanistan_data['population'].values[0], label='Afghanistan Net Migration', color='green')

australia_bar = ax.bar('Australia', australia_data['population'].values[0], label='Australia Population', color='red')
australia_migration_bar = ax.bar('Australia', australia_data['netMigration'].values[0], bottom=australia_data['population'].values[0], label='Australia Net Migration', color='purple')

# 添加图例
plt.legend()

# 添加标题和标签
plt.title('Population and Net Migration in 2023: Afghanistan vs Australia')
plt.xlabel('Country')
plt.ylabel('Number of People')

# 显示图表
plt.show()

import pandas as pd
import matplotlib.pyplot as plt

# 读取数据
file_path = r"C:\Users\luojinying\Desktop\world_pop_mig_186_countries.csv"
data = pd.read_csv(file_path)

# 数据清洗
# 确保年份、人口和迁移人口列的数据类型正确
data['year'] = pd.to_numeric(data['year'], errors='coerce')
data['population'] = pd.to_numeric(data['population'], errors='coerce')
data['netMigration'] = pd.to_numeric(data['netMigration'], errors='coerce')

# 去除可能的缺失值
data.dropna(subset=['year', 'population', 'netMigration'], inplace=True)

# 筛选出阿富汗和澳大利亚的数据
afghanistan_data = data[data['country'] == 'Afghanistan']
australia_data = data[data['country'] == 'Australia']

# 设置图表
plt.figure(figsize=(14, 8))

# 绘制面积图
plt.fill_between(afghanistan_data['year'], afghanistan_data['population'], color='blue', label='Afghanistan Population')
plt.fill_between(afghanistan_data['year'], afghanistan_data['population'] + afghanistan_data['netMigration'], color='green', alpha=0.3, label='Afghanistan Population + Net Migration')

plt.fill_between(australia_data['year'], australia_data['population'], color='red', label='Australia Population')
plt.fill_between(australia_data['year'], australia_data['population'] + australia_data['netMigration'], color='purple', alpha=0.3, label='Australia Population + Net Migration')

# 添加图例
plt.legend()

# 添加标题和标签
plt.title('Cumulative Population and Net Migration: Afghanistan vs Australia (1960-2023)')
plt.xlabel('Year')
plt.ylabel('Cumulative Number of People')

# 显示图表
plt.grid(True)
plt.show()

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# 读取和清洗数据
def load_and_clean_data(file_path):
    data = pd.read_csv(file_path)
    data['year'] = pd.to_numeric(data['year'], errors='coerce')
    data['population'] = pd.to_numeric(data['population'], errors='coerce')
    data['netMigration'] = pd.to_numeric(data['netMigration'], errors='coerce')
    data.dropna(subset=['year', 'population', 'netMigration'], inplace=True)
    return data

# 筛选出阿富汗和澳大利亚的数据
def filter_data(data, countries):
    return data[data['country'].isin(countries)]

# 读取数据
file_path = r"C:\Users\luojinying\Desktop\world_pop_mig_186_countries.csv"
data = load_and_clean_data(file_path)

# 筛选数据
selected_countries = ['Afghanistan', 'Australia']
data_filtered = filter_data(data, selected_countries)

# 箱型图
plt.figure(figsize=(10, 6))
sns.boxplot(x='country', y='population', data=data_filtered, hue='country', palette='Set1', legend=False)
plt.title('Boxplot of Population')
plt.show()

# 热力图
plt.figure(figsize=(10, 6))
pivot_table = data_filtered.pivot_table(index='year', columns='country', values='population')
sns.heatmap(pivot_table, cmap='coolwarm', annot=True, cbar_kws={'label': 'Population'})
plt.title('Heatmap of Population for Afghanistan and Australia')
plt.xlabel('Country')
plt.ylabel('Year')
plt.show()

# 饼图
for country in selected_countries:
    country_data = data_filtered[data_filtered['country'] == country]
    sizes = [country_data['population'].iloc[-1], country_data['netMigration'].iloc[-1]]
    labels = [f'{country} Population', f'{country} Net Migration']
    plt.figure(figsize=(8, 8))
    plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140)
    plt.title(f'Pie Chart of Population and Net Migration (2023) for {country}')
    plt.show()

# 筛选出阿富汗和澳大利亚的数据
selected_countries = ['Afghanistan', 'Australia']
data_filtered = data[data['country'].isin(selected_countries)]

# 散点图
plt.figure(figsize=(10, 6))
for country in selected_countries:
    country_data = data_filtered[data_filtered['country'] == country]
    plt.scatter(country_data['population'], country_data['netMigration'], label=country)

plt.title('Scatter Plot of Population vs Net Migration')
plt.xlabel('Population')
plt.ylabel('Net Migration')
plt.legend()
plt.grid(True)
plt.show()